<template>
  <div>
    <p><el-button type="primary" @click="openAddType('添加')">添加类型</el-button></p>
    <div>
      <div class="tableDiv">
        <el-table ref="multipleTableRef" :data="tableData" style="width: 360px" border
          @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" />
          <el-table-column label="类型名称" width="120">
            <template #default="scope">{{ scope.row.name }}</template>
          </el-table-column>
          <el-table-column label="操作" width="180">
            <template #default="scope">
              <el-button link type="primary" size="small" @click.prevent="compileTypeName('编辑', scope.row)">
                编辑
              </el-button>
              <el-button link type="primary" size="small" @click.prevent="delectTypeName(scope.row)">
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <p><el-button type="primary" @click="everyDelect">选中批量删除</el-button></p>
    <div class="page">
      <el-pagination v-model:current-page="currentPage4" v-model:page-size="pageSize4" :page-sizes="[10, 50, 100]"
        layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="handleSizeChange"
        @current-change="handleCurrentChange" />
    </div>
    <!-- 添加+编辑弹框 -->
    <div>
      <el-dialog v-model="AddDialogVisible" :title="compile + '类型'" width="500">
        <span>类型名称:<el-input v-model="typeName" style="width: 240px" placeholder="请输入" /></span>
        <template #footer>
          <div class="dialog-footer">
            <el-button type="primary" @click="AddDialogSubMit">保存</el-button>
            <el-button @click="AddDialogVisible = false">
              取消
            </el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script setup>
import { ref, onBeforeMount } from 'vue'
import { ElMessage } from 'element-plus'
import { productTypeCreate, productTypeUpdate, productTypeDelete, productTypeList, productTypeDeleteBatch } from '@/api/index'
const AddDialogVisible = ref(false)
const typeName = ref('')
const openAddType = (val) => {
  compile.value = val
  typeName.value = ''
  AddDialogVisible.value = true
}
// 首次
onBeforeMount(() => {
  getList()
})
const params = ref({ pageSize: '100', pageNum: '1' })
// List
const getList = () => {
  productTypeList(params.value).then(res => {
    const { data: { data } } = res
    console.log('data')
    console.log(data)
    total.value = data.total
    tableData.value = data.list
  })
}
// table
const multipleSelection = ref([])
const tableData = ref([])
const handleSelectionChange = (val) => {
  multipleSelection.value = []
  val.forEach(element => {
    multipleSelection.value.push(element.id)
  })
}
// 删除
const delectTypeName = (row) => {
  productTypeDelete(row.id).then(res => {
    ElMessage.success('删除成功')
    getList()
  })
}
// 批量删除
const everyDelect = () => {
  if (multipleSelection.value.length > 0) {
    productTypeDeleteBatch(multipleSelection.value).then(res => {
      ElMessage.success('删除成功')
      getList()
    })
  } else {
    ElMessage.error('需要先选中数据')
  }
}
// 编辑
const compile = ref('')
const rowId = ref('')
const compileTypeName = (val, row) => {
  compile.value = val
  rowId.value = row.id
  typeName.value = row.name
  AddDialogVisible.value = true
}
// 添加
const AddDialogSubMit = (val) => {
  if (compile.value === '添加') {
    productTypeCreate({ name: typeName.value }).then(res => {
      ElMessage.success('添加成功')
      getList()
      AddDialogVisible.value = false
    })
  } else if (compile.value === '编辑') {
    productTypeUpdate({ name: typeName.value }, rowId.value).then(res => {
      ElMessage.success('修改成功')
      getList()
      AddDialogVisible.value = false
    })
  }
}
// 分页
const currentPage4 = ref(1)
const pageSize4 = ref(10)
const total = ref(0)
const handleSizeChange = (val) => {
  console.log(`${val} items per page`)
  params.value.pageSize = val
  getList()
}
const handleCurrentChange = (val) => {
  console.log(`current page: ${val}`)
  params.value.pageNum = val
  getList()
}
</script>

<style lang='less' scoped></style>
